package com.avecsys.quotemanager.mbeans.menu;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.icefaces.ace.model.table.RowStateMap;

import com.avecsys.quotemanager.pojos.menu.RootMenuEntity;
import com.avecsys.quotemanager.pojos.menu.SubMenuEntity;
import com.avecsys.quotemanager.service.menu.MenuManagerService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.icesoft.faces.component.menubar.MenuItem;
import com.icesoft.faces.context.effects.JavascriptContext;

public class MenuAccordionMBean {

	private MenuManagerService menuManagerService;

	private List<RootMenuEntity> rootMenuEntityList;

	private List<AccordionMenuItem> accordionMenuItemList = new ArrayList<AccordionMenuItem>();

	/**
	 * @return the accordionMenuItemList
	 */
	public List<AccordionMenuItem> getAccordionMenuItemList() {
		return accordionMenuItemList;
	}

	/**
	 * @param accordionMenuItemList
	 *            the accordionMenuItemList to set
	 */
	public void setAccordionMenuItemList(
			List<AccordionMenuItem> accordionMenuItemList) {
		this.accordionMenuItemList = accordionMenuItemList;
	}

	/**
	 * @return the menuManagerService
	 */
	public MenuManagerService getMenuManagerService() {
		return menuManagerService;
	}

	/**
	 * @param menuManagerService
	 *            the menuManagerService to set
	 */
	public void setMenuManagerService(MenuManagerService menuManagerService) {
		this.menuManagerService = menuManagerService;
		initilizeMenu();
	}

	/**
	 * @return the rootMenuEntityList
	 */
	public List<RootMenuEntity> getRootMenuEntityList() {
		return rootMenuEntityList;
	}

	/**
	 * @param rootMenuEntityList
	 *            the rootMenuEntityList to set
	 */
	public void setRootMenuEntityList(List<RootMenuEntity> rootMenuEntityList) {
		this.rootMenuEntityList = rootMenuEntityList;
	}

	private RowStateMap rowStateMap = new RowStateMap();

	private List<AccordionMenuItem> menuItems = new ArrayList<AccordionMenuItem>();

	public MenuAccordionMBean() {
		super();
	}

	/**
	 * @return the menuItems
	 */
	public List<AccordionMenuItem> getMenuItems() {
		return menuItems;
	}

	/**
	 * @param menuItems
	 *            the menuItems to set
	 */
	public void setMenuItems(List<AccordionMenuItem> menuItems) {
		this.menuItems = menuItems;
	}

	/**
	 * @return the rowStateMap
	 */
	public RowStateMap getRowStateMap() {
		return rowStateMap;
	}

	/**
	 * @param rowStateMap
	 *            the rowStateMap to set
	 */
	public void setRowStateMap(RowStateMap rowStateMap) {
		this.rowStateMap = rowStateMap;
	}

	public void actionSelectedMenuItem(ActionEvent actionEvent) {
		MessageBoardUtil.resetMessageBoard();
		MenuItem menuItem = (MenuItem) actionEvent.getSource();
		menuItem.setStyleClass("samClass");
		JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(),
				"updateFrame('" + menuItem.getLink() + "');");
	}

	public void initilizeMenu() {
		List<RootMenuEntity> rootMenuEntityList = menuManagerService
				.getAllMenus();
		buildMenuBar(rootMenuEntityList);
	}

	public void buildMenuBar(List<RootMenuEntity> rootMenuEntityList) {
		getAccordionMenuItemList().clear();
		int index = 0;
		for (RootMenuEntity rootMenuEntity : rootMenuEntityList) {
			AccordionMenuItem accordionMenuItem = new AccordionMenuItem();
			accordionMenuItem.setRootMenuEntity(rootMenuEntity);
			List<MenuItem> menuItemList = new ArrayList<MenuItem>();
			List<SubMenuEntity> subMenusList = rootMenuEntity
					.getSubMenuEntityList();
			for (SubMenuEntity subMenuEntity : subMenusList) {
				index++;
				MenuItem menuItem = new MenuItem();
				menuItem.setId("menuItem" + index);
				menuItem.setLink(subMenuEntity.getLinkPath());
				menuItem.setValue(subMenuEntity.getSubMenuName());
				menuItem.setLink(subMenuEntity.getLinkPath());
				menuItem.setTarget("contentFrame");
				if (subMenuEntity.getIcon() != null
						&& subMenuEntity.getIcon().trim().length() > 0) {
					menuItem.setIcon(subMenuEntity.getIcon());
				}
				menuItemList.add(menuItem);
			}
			accordionMenuItem.setMenuItemList(menuItemList);
			getAccordionMenuItemList().add(accordionMenuItem);
		}
	}
}
